package com.xyy.saas.payment.service.impl;

import com.xyy.saas.payment.dao.mapper.*;
import com.xyy.saas.payment.dao.model.*;
import com.xyy.saas.payment.service.OrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author wangtianqi
 * @date 2020/5/13 15:03
 */
@Service
@DependsOn({"payResultOrderMapper", "prePayOrderMapper", "refundOrderMapper"})
public class OrderServiceImpl implements OrderService {

    @Autowired
    private PayResultOrderMapper payResultOrderMapper;

    @Autowired
    private PrePayOrderMapper prePayOrderMapper;

    @Autowired
    private RefundOrderMapper refundOrderMapper;

    @Autowired
    private OrderTransDataMapper orderTransDataMapper;

    @Autowired
    private LoanRepayPlanMapper repayPlanMapper;

    @Autowired
    private LoanRepayPlanDetailMapper repayPlanDetailMapper;


    @Override
    public PayResultOrder selectPayResultOrderByPayNoAndChannel(String payNo, String channel){
        return payResultOrderMapper.selectByPayNoAndChannel(payNo, channel);
    }

    @Override
    public PayResultOrder selectByChannelChargeNo(String channelChargeNo) {
        return payResultOrderMapper.selectByChannelChargeNo(channelChargeNo);
    }

    @Override
    public List<PayResultOrder> selectPayResultOrderListByPayNo(String payNo) {
        return payResultOrderMapper.selectByPayNo(payNo);
    }

    @Override
    public List<PayResultOrder> selectPayResultOrderListByPayNos(List<String> payNos) {
        return payResultOrderMapper.selectByPayNos(payNos);
    }

    @Override
    public int insertPayResultOrderSelective(PayResultOrder payResultOrder){
        return payResultOrderMapper.insertSelective(payResultOrder);
    }

    @Override
    public List<PayResultOrder> selectPayResultOrderListByCondition(String payNo, String businessOrderType, String payer, String channelTransactionNo, String channel, Date beginTime, Date endTime, int page, int pageSize) {
        return payResultOrderMapper.selectPayResultOrderListByCondition(payNo, businessOrderType, payer, channelTransactionNo, channel, beginTime, endTime, page, pageSize);
    }
    @Override
    public int selectPayResultOrderCountByCondition(String payNo, String businessOrderType, String payer, String channelTransactionNo, String channel, Date beginTime, Date endTime) {
        return payResultOrderMapper.selectPayResultOrderCountByCondition(payNo, businessOrderType, payer, channelTransactionNo, channel, beginTime, endTime);
    }
    @Override
    public List<PrePayOrder> getPrePayOrderByBusinessOrderNo(String businessOrderNo){
        return prePayOrderMapper.selectByBusinessOrderNo(businessOrderNo);
    }

    @Override
    public PrePayOrder getPrePayOrderByPayNo(String payNo){
        return prePayOrderMapper.getPrePayOrderByPayNo(payNo);
    }

    @Override
    public List<PrePayOrder> getPrePayOrderByPayNos(List<String> payNos){
        return prePayOrderMapper.getPrePayOrderByPayNos(payNos);
    }

    @Override
    public List<RefundOrder> selectRefundOrderListByCondition(String payNo, String businessOrderType, String payer, String channelTransactionNo, String channel, Date beginTime, Date endTime, int page, int pageSize) {
        return refundOrderMapper.selectRefundOrderListByCondition(payNo, businessOrderType, payer, channelTransactionNo, channel, beginTime, endTime, page, pageSize);
    }

    @Override
    public int selectRefundOrderCountByCondition(String payNo, String businessOrderType, String payer, String channelTransactionNo, String channel, Date beginTime, Date endTime) {
        return refundOrderMapper.selectRefundOrderCountByCondition(payNo, businessOrderType, payer, channelTransactionNo, channel, beginTime, endTime);
    }

    @Override
    public List<RefundOrder> selectRefundOrderListByPayNo(String payNo) {
        return refundOrderMapper.selectListByPayNo(payNo);
    }

    @Override
    public List<RefundOrder> selectNoneRoyaltyRefundOrders(LocalDateTime beginTime, LocalDateTime endTime) {
        if (beginTime == null || endTime == null) {
            return Collections.emptyList();
        }
        return refundOrderMapper.selectNoneRoyaltyRefundOrders(beginTime, endTime);
    }

    @Override
    public int updatePrePayOrderById(PrePayOrder record) {
        return prePayOrderMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public RefundOrder selectByRefundNo(String refundNo) {
        return refundOrderMapper.selectByRefundNo(refundNo);
    }

    /**
     * 根据签约协议号查询预支付订单信息
     *
     * @param contractCode
     * @return
     */
    @Override
    public List<PrePayOrder> selectByContractCode(String contractCode) {
        if(StringUtils.isEmpty(contractCode)) {
            return new ArrayList<>();
        }
        return prePayOrderMapper.selectByContractCode(contractCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsertRepayPlans(List<OrderTransDataPo> transDataPos, List<LoanRepayPlanPo> repayPlanPos, List<LoanRepayPlanDetailPo> detailPos) {
        if (transDataPos == null || transDataPos.isEmpty()) {
            return;
        }

        orderTransDataMapper.batchInsertSelective(transDataPos);
        if (!(repayPlanPos == null || repayPlanPos.isEmpty())) {
            repayPlanMapper.batchInsert(repayPlanPos);
        }

        if (!(detailPos == null || detailPos.isEmpty())) {
            repayPlanDetailMapper.batchInsert(detailPos);
        }

    }
}
